
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="74rem" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="平台" prop="platformId">
        <el-select clearable v-model="formData.platformId" placeholder="请选择">
          <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channelId">
        <el-select clearable v-model="formData.channelId" placeholder="请选择">
          <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select clearable v-model="formData.bizType" placeholder="请选择">
          <el-option v-for="item in bizTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车型" prop="vehicleTypeId">
        <el-select filterable clearable v-model="formData.vehicleTypeId" placeholder="请选择">
          <el-option v-for="item in carTypeArr" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="cityId">
        <el-select filterable clearable v-model="formData.cityId" placeholder="请选择" @change="handleCityChange">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker style="width: 100%" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD 00:00:00" placeholder="请选择开始时间" v-model="formData.startTime" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker style="width: 100%" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD 23:59:59" placeholder="请选择结束时间" v-model="formData.endTime" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input placeholder="请输入" v-model="formData.remark"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :value="1">上线</el-radio>
          <el-radio :value="0">下线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="配置">
        <div class="flex_box" v-for="(item, i) in configArr" :key="item.id">
          <el-input-number style="width: 90%" placeholder="请输入开始评分" v-model="item.scoreFrom"></el-input-number>
          <el-input-number style="width: 90%" placeholder="请输入结束评分" v-model="item.scoreTo"></el-input-number>
          <el-input-number style="width: 90%" placeholder="请输入奖励" v-model="item.reward"></el-input-number>
          <el-button type="danger" @click="handleRemove(i)">删除</el-button>
          <el-button style="position: absolute; right: -80px" v-if="i == configArr.length - 1" type="primary" @click="handleAdd">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import ActivityApi from "@/api/activity";
import CommonApi from "@/api/common";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
const carOptions = ref();
const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref(null);
let formData: any = ref({
  id: "",
  platformId: undefined,
  channelId: undefined,
  bizType: undefined,
  vehicleTypeId: undefined,
  cityId: undefined,
  status: "",
  startTime: "",
  endTime: "",
  remark: "",
});
const rules: any = reactive({
  platformId: [{ required: true, message: "请选择!" }],
  channelId: [{ required: true, message: "请选择!" }],
  bizType: [{ required: true, message: "请选择!" }],
  vehicleTypeId: [{ required: true, message: "请选择!" }],
  cityId: [{ required: true, message: "请选择!" }],
  status: [{ required: true, message: "请选择!" }],
  startTime: [{ required: true, message: "请选择!" }],
  endTime: [{ required: true, message: "请选择!" }],
});
const configArr: any = ref([
  {
    reward: "",
    scoreFrom: "",
    scoreTo: "",
    id: Date.now() + Math.random().toString(16).slice(2),
  },
]);
const ifShow = ref(false);
const modelTitle = ref();
const cityOptions = ref();
function edit(title: any, row: any) {
  modelTitle.value = title;
  ifShow.value = true;
  if (title == "编辑") {
    configArr.value = JSON.parse(JSON.stringify(row.config));
  }
  if (title == "新增") {
    formData.value = {
      id: "",
      platformId: undefined,
      channelId: undefined,
      bizType: undefined,
      vehicleTypeId: undefined,
      cityId: undefined,
      status: "",
      startTime: "",
      endTime: "",
      remark: "",
    };
  }
  //赋值
  Object.assign(formData.value, row);
}

function handleAdd(code: any) {
  configArr.value.push({
    reward: "",
    scoreFrom: "",
    scoreTo: "",
    id: Date.now() + Math.random().toString(16).slice(2),
  });
}
function handleRemove(index: any) {
  if (configArr.value.length !== 1) {
    configArr.value.splice(index, 1);
  }
}

/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await formRef.value.validate((valid: any, fields: any) => {
    if (valid) {
      let model: any = formData.value;
      model.config = configArr.value;
      ActivityApi.edit(model).then((res: any) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

//城市下拉框点击事件
function handleCityChange(value: any) {
  formData.valuecityName = cityOptions.value.find((item: any) => item.id == value).name;
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  configArr.value = [
    {
      reward: "",
      scoreFrom: "",
      scoreTo: "",
      id: Date.now() + Math.random().toString(16).slice(2),
    },
  ];
  ifShow.value = false;
}

onMounted(() => {
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});

defineExpose({
  edit,
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  margin-bottom: 14px;
  position: relative;
}
</style>
